package com.bing.bingPro.controller.zd;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bing.bingPro.common.BaseResult;
import com.bing.bingPro.controller.dto.ZdBasicOrderDto;
import com.bing.bingPro.controller.vo.ZdBasicInfoVo;
import com.bing.bingPro.controller.vo.ZdCheckOrderVo;
import com.bing.bingPro.controller.vo.ZdSupportOrderVo;
import com.bing.bingPro.service.ZdBasicInfoService;
import com.bing.bingPro.service.ZdCheckOrderService;
import com.bing.bingPro.service.ZdSupportOrderService;
import com.bing.bingPro.util.ExcelData;
import com.bing.bingPro.util.WebExcelUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@RestController
public class ZdpdController {

    /*    @Resource
        private ZdRepairOrderService zdRepairOrderService;*/
    @Resource
    private ZdBasicInfoService zdpdService;
    @Resource
    private ZdCheckOrderService zdCheckOrderService;

    @PostMapping("/getCountry")
    public BaseResult<List<String>> getCountry() {
        return new BaseResult<>(this.zdpdService.getCountry());
    }

    @PostMapping("/zdList")
    public BaseResult<List<ZdBasicInfoVo>> cpList(@RequestBody ZdBasicOrderDto zdBasicOrderDto) {
        List<ZdBasicInfoVo> result = zdpdService.zdBasicList(zdBasicOrderDto);
        return new BaseResult<>(result);
    }

    @PostMapping("/zdList1")
    public BaseResult<IPage<ZdBasicInfoVo>> cpList1(@RequestBody final ZdBasicOrderDto zdBasicOrderDto) {
        final IPage<ZdBasicInfoVo> result = zdpdService.zdBasicIPageList(zdBasicOrderDto);
        return new BaseResult<>(result);
    }

    @PostMapping("/zdCheckList")
    public BaseResult<IPage<ZdCheckOrderVo>> zdCheckList(@RequestBody ZdBasicOrderDto zdBasicOrderDto) {
        IPage<ZdCheckOrderVo> result = zdCheckOrderService.zdCheckPageList(zdBasicOrderDto);
        return new BaseResult<>(result);
    }

    @PostMapping("/zdCheckList2")
    public BaseResult<List<ZdCheckOrderVo>> zdCheckList2(@RequestBody ZdBasicOrderDto zdBasicOrderDto) {
        List<ZdCheckOrderVo> zdBasicInfoVos = this.zdCheckOrderService.zdCheckList(zdBasicOrderDto);
        return new BaseResult<>(zdBasicInfoVos);
    }

    @PostMapping("/exportBasic")
    public void export(HttpServletResponse response, @RequestBody ZdBasicOrderDto zdBasicOrderDto) {
        try {
            Assert.notBlank(zdBasicOrderDto.getTime(), "not time");
            // 查询数据
            List<ZdBasicInfoVo> zdBasicInfoVos = this.zdpdService.zdBasicList(zdBasicOrderDto);
            // 将数据分割成每50W数据为一个sheet页
            int batchSize = 1000;
            List<ExcelData> excelDataList = IntStream.range(0, (zdBasicInfoVos.size() + batchSize - 1) / batchSize)
                    .mapToObj(i -> {
                        int startIndex = i * batchSize;
                        int endIndex = Math.min(startIndex + batchSize, zdBasicInfoVos.size());
                        List<ZdBasicInfoVo> subList = zdBasicInfoVos.subList(startIndex, endIndex);
                        String sheetName = "综调平台基础信息列表第" + (i + 1)+"页"; // 设置 sheet 名称
                        return new ExcelData(sheetName, ZdBasicInfoVo.class, subList);
                    })
                    .collect(Collectors.toList());

            // 写出到浏览器
            WebExcelUtil.download(response, "综调平台基础信息列表导出" + zdBasicOrderDto.getTime(), excelDataList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Resource
    private ZdSupportOrderService zdSupportOrderService;

    /**
     * 综调支撑工单(会话)列表
     *
     * @param zdBasicOrderDto
     * @return
     */
    @PostMapping("/zdSupportList")
    public BaseResult<IPage<ZdSupportOrderVo>> zdSupportList(@RequestBody ZdBasicOrderDto zdBasicOrderDto) {
        if (CharSequenceUtil.isEmpty(zdBasicOrderDto.getTime())) {
            return new BaseResult<>(404, "参数有误，time是必填参数！");
        }
        return new BaseResult<>(this.zdSupportOrderService.zdSupportPageList(zdBasicOrderDto));
    }
}
